package com.hjs.security;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
public class DesFile {
    private static String keyFileName = "des_key.xml";
    private static final String ALGORITHM = "DES";
    private static final SecureRandom sr = new SecureRandom();
    public static void main(String[] args) {
       //  encryptFile("D:\\plain.txt", "D:\\plain_encrypt.txt", "defastronomer");
        // decryptFile("D:\\plain_encrypt.txt", "D:\\plain_encrypt_decrypt.txt", "defastronomer");
    }
    public static void storeSecretKey() {
        FileOutputStream out = null;
        ObjectOutputStream oout = null;
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
            keyGenerator.init(sr);
            SecretKey secretKey = keyGenerator.generateKey();
            out = new FileOutputStream(keyFileName);
            oout = new ObjectOutputStream(out);
            oout.writeObject(secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                oout.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static String readFileByLine(String fileName){
        File file=new File(fileName);
        BufferedReader reader=null;
        String fileContent="";
        try {
            reader=new BufferedReader(new FileReader(file));
            String tempString=null;
            int line=1;
            while ((tempString=reader.readLine())!=null){
                System.out.println("line"+line+":"+tempString);
                fileContent+=tempString;
                line++;
            }
            return fileContent;
        }catch (Exception e){
            e.printStackTrace();
            return "读取文件异常";
        }finally {
            if (reader!=null){
                try{
                    reader.close();
                }catch (Exception e){
                }
            }
        }
    }
    public static SecretKey loadSecretKey() {
        SecretKey secretKey = null;
        try {
            FileInputStream in = new FileInputStream(keyFileName);
            ObjectInputStream oin = new ObjectInputStream(in);
            secretKey = (SecretKey) oin.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return secretKey;
    }
    private static SecretKey generateSecretKey(String key) {
        SecretKey secretKey = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = key.getBytes();
            md.update(bytes, 0, bytes.length);
            byte[] mdBytes = md.digest();
            byte[] truncatedBytes = Arrays.copyOf(mdBytes, 8);
            DESKeySpec keySpec = new DESKeySpec(truncatedBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
            secretKey = keyFactory.generateSecret(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return secretKey;
    }
    public static boolean encryptFile(String file, String newFile, String key) {
        SecretKey secretKey = generateSecretKey(key);
        boolean flag=encryptFile(file, newFile, secretKey);
        return flag;
    }
    private static boolean encryptFile(String file, String newFile, SecretKey secretKey) {
        InputStream in = null;
        CipherInputStream cin = null;
        OutputStream out = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
            in = new FileInputStream(file);
            cin = new CipherInputStream(in, cipher);
            out = new FileOutputStream(newFile);
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = cin.read(buffer)) > 0) {
                out.write(buffer, 0, count);
            }
            System.out.println("加密成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                cin.close();
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static boolean decryptFile(String file, String newFile, String key) {
        SecretKey secretKey = generateSecretKey(key);
        boolean flag=decryptFile(file, newFile, secretKey);
        return flag;
    }
    private static boolean decryptFile(String file, String newFile, SecretKey secretKey) {
        InputStream in = null;
        CipherOutputStream cout = null;
        OutputStream out = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
            in = new FileInputStream(file);
            out = new FileOutputStream(newFile);
            cout = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = in.read(buffer)) > 0) {
                cout.write(buffer, 0, count);
            }
            System.out.println("解密成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                cout.close();
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
